package com.lb.activity.sys.service.impl;

import com.lb.activity.mapper.DeptMapper;
import com.lb.activity.po.Dept;
import com.lb.activity.po.DeptExample;
import com.lb.activity.sys.service.DeptService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.util.SnowflakeIdWorker;
import com.lb.activity.util.emumdata.DataEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Xiao
 * @create 2018-11-12 17:01
 **/
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Resource
    private SnowflakeIdWorker idWorker;

    @Override
    public List<Dept> AllDepts() {
        DeptExample example=new DeptExample();
        DeptExample.Criteria  criteria= example.createCriteria();
//        criteria.andStatusNotEqualTo(DataEnum.DELETE_STATUS.getStatus());
        return deptMapper.selectByExample(example);
    }

    @Override
    public ResultUtil addDept(Dept dept) {
        dept.setId(idWorker.nextId());
        dept.setStatus(DataEnum.INSERT_STATUS.getStatus());
        int i = deptMapper.insertSelective(dept);
        return ResultUtil.ok(i>0);
    }

    @Override
    public int repet(String deptName) {
        DeptExample example=new DeptExample();
        DeptExample.Criteria  criteria= example.createCriteria();
        criteria.andDeptNameEqualTo(deptName);
        int count = deptMapper.countByExample(example);
        return count;
    }

    @Override
    public Dept getById(Long id) {
        return deptMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultUtil updateDept(Dept dept) {
        deptMapper.updateByPrimaryKeySelective(dept);
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil removeDept(Long id) {
        deptMapper.deleteDept(id);
        return ResultUtil.ok();
    }

    @Override
    public String deptTreeI() {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        DeptExample example=new DeptExample();
        DeptExample.Criteria  criteria= example.createCriteria();
        criteria.andStatusNotEqualTo(DataEnum.DELETE_STATUS.getStatus());
        criteria.andParentIdEqualTo(0L);
        List<Dept> depts = deptMapper.selectByExample(example);
        int j = 1;
        for (Dept dept : depts) {
            DeptExample examplec = new DeptExample();
            DeptExample.Criteria  criteriac= examplec.createCriteria();
            sb.append("{");
            sb.append("\"text\":");
            sb.append("\""+dept.getDeptName()+"\",");
            sb.append("\"id\":");
            sb.append("\""+dept.getId()+"\"");
            criteriac.andStatusNotEqualTo(DataEnum.DELETE_STATUS.getStatus());
            criteriac.andParentIdEqualTo(dept.getId());
            List<Dept> deptsChild = deptMapper.selectByExample(examplec);
            int i = 1;
            if(deptsChild.size()>0){
                sb.append(",");
                sb.append("\"nodes\":[");
            }else{
                sb.append("},");
            }
            for (Dept deptC : deptsChild) {
                sb.append("{\"text\":");
                sb.append("\""+deptC.getDeptName()+"\",");
                sb.append("\"id\":");
                sb.append("\""+deptC.getId()+"\"");
                sb.append("}");
                if(i < deptsChild.size()){
                    sb.append(",");
                }else if(deptsChild.size() == i){
                    if(depts.size() == j){
                        sb.append("]");
                    }else{
                        sb.append("]},");
                    }
                }
                i++;
            }
            if( i < deptsChild.size()){
                sb.append("]}");
                sb.append(",");
            }
            sb.append("\n");
            j++;
        }
        sb.append("}]");
        System.out.println(sb.toString());
        return sb.toString();
    }


}
